﻿using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class TimKiem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loadGioHang();
            loadGrid();
        }
        public void loadGioHang()
        {

            var giohang = Session["listGioHang"] as List<SanPham>;
            if (giohang != null)
            {
                long longTongSoLuong = 0;
                long longTongTien = 0;
                foreach (var sanpham in giohang)
                {
                    longTongSoLuong += sanpham.SoLuongMua;
                    longTongTien += sanpham.TongSoTien;
                }
                lbTongSanPham.InnerText = longTongSoLuong.ToString();
                lbTongTien.InnerText = longTongTien.ToString();
            }
            else
            {
                lbTongSanPham.InnerText = "0";
                lbTongTien.InnerText = "0";
            }
        }
        public void loadGrid()
        {
            lbLoaiSanPham.InnerText = "KẾT QUẢ TÌM KIẾM";
            string tukhoa = Request.QueryString["tukhoa"];
            long giatu = (IsNumeric(Request.QueryString["giatu"]) == false ? 0 : Convert.ToInt64(Request.QueryString["giatu"]));
            long giaden = (IsNumeric(Request.QueryString["giaden"]) == false ? 0 : Convert.ToInt64(Request.QueryString["giaden"]));
            using (var userService = new SanPhamSrv.SanPhamServiceClient())
            {
                var lst = userService.TimKiemSanPham(tukhoa,giatu,giaden);
                lvDanhSach.DataSource = lst;
                lvDanhSach.DataBind();
            }
        }
        public static bool IsNumeric(string text)
        {
            return string.IsNullOrEmpty(text) ? false :
                    Regex.IsMatch(text, @"^\s*\-?\d+(\.\d+)?\s*$");
        }
    }
}